package com.springboot.mall.service.impl;

import com.springboot.mall.common.Constant;
import com.springboot.mall.exception.MallException;
import com.springboot.mall.exception.MallExceptionEnum;
import com.springboot.mall.model.entity.Cart;
import com.springboot.mall.model.entity.Product;
import com.springboot.mall.model.mappers.CartMapper;
import com.springboot.mall.model.mappers.ProductMapper;
import com.springboot.mall.model.vo.CartVO;
import com.springboot.mall.model.vo.CategoryVO;
import com.springboot.mall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 *
 *
 *@data 2022-05-14
 */
@Service("cartService")
public class CartServiceImpl implements CartService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CartMapper cartMapper;


    @Override
    public List<CartVO> add(Integer userId, Integer productId, Integer count) {
        validProduct(productId,count);

        //如果商品不在购物车中
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null){
            //这个商品之前不在购物车里，需要新增一个记录
            cart = new Cart();
            cart.setProductId(productId);
            cart.setUserId(userId);
            cart.setQuantity(count);
            cart.setSelected(Constant.Cart.CHECKED);
            cartMapper.insertSelective(cart);
        }else{
            //这个商品已经在购物车里了，则数量相加
            count = cart.getQuantity() + count;
            Cart cartNew = new Cart();
            cartNew.setQuantity(count);
            cartNew.setId(cart.getId());
            cartNew.setProductId(cart.getProductId());
            cartNew.setUserId(cart.getUserId());
            cartNew.setSelected(Constant.Cart.CHECKED);
            cartMapper.updateByPrimaryKeySelective(cartNew);
        }
        return this.list(userId);
    }

    //验证这次添加是否合法
    /*
    * 首先将这件商品查出来
    *
    *
    * */
    private void validProduct(Integer productId, Integer count){
        Product product = productMapper.selectByPrimaryKey(productId);
        //判断商品是否存在，商品是否上架
        if (product == null || product.getStatus().equals(Constant.SaleStatus.NOT_SALE)){
            throw new MallException(MallExceptionEnum.NOT_SALE);
        }

        //判断商品库存
        if (count > product.getStock()){
            throw  new MallException(MallExceptionEnum.NOT_ENOUGH);
        }

    }


    @Override
    public List<CartVO> list(Integer userId) {
        List<CartVO> cartVOS = cartMapper.selectList(userId);
        for (int i = 0; i <cartVOS.size(); i++) {
            //有指针的味道了
            CartVO cartVO = cartVOS.get(i);
            cartVO.setTotalPrice(cartVO.getQuantity() * cartVO.getPrice());
        }
        return cartVOS;
    }

    @Override
    public List<CartVO> update(Integer userId, Integer productId, Integer count) {
        //如果商品不在购物车中
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null){
            //这个商品之前不在购物车里,更新失败
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }else{
            //这个商品已经在购物车里了，则数量相加
            Cart cartNew = new Cart();
            cartNew.setQuantity(count);
            cartNew.setId(cart.getId());
            cartNew.setProductId(cart.getProductId());
            cartNew.setUserId(cart.getUserId());
            cartNew.setSelected(Constant.Cart.CHECKED);
            cartMapper.updateByPrimaryKeySelective(cartNew);
        }
        return this.list(userId);
    }

    @Override
    public List<CartVO> delete(Integer userId, Integer productId) {
        //如果商品不在购物车中
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null){
            //这个商品之前不在购物车里,删除失败
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }else{
            cartMapper.deleteByPrimaryKey(cart.getId());
        }
        return this.list(userId);
    }

    @Override
    public List<CartVO> updateSelectedOne(Integer userId, Integer productId, Integer selected) {
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if (cart == null){
            //这个商品之前不在购物车里,删除失败
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }else{
            int i = cartMapper.updateSelectedOne(userId, productId, selected);
            if (i == 0){
                throw new MallException(MallExceptionEnum.UPDATE_FAILED);
            }
        }
        return this.list(userId);
    }

    @Override
    public List<CartVO> updateSelectedAll(Integer userId, Integer selected) {
        cartMapper.updateSelectedOne(userId,null, selected);
        return this.list(userId);
    }




}
